<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.sinosoft.moniter.utils.StringUtils"%>
<%@page import="com.sinosoft.moniter.utils.DateUtils"%>
<%@page import="java.util.Iterator"%>
<%@page import="com.sinosoft.moniter.utils.Config"%>
<%@page import="java.util.List"%>
<%@page import="com.jcraft.jsch.ChannelSftp.LsEntry"%>
<%@page import="java.util.Vector"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	Vector<LsEntry> v = (Vector<LsEntry>) session.getAttribute("v");
	String vv_path = session.getAttribute("vv_path") + "";
	String filter=request.getParameter("filter")+"";
	String only=request.getParameter("only")+"";
	System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis())));
	System.out.println("vv_path:"+vv_path);
%>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ServerMonitor</title>
<link href="<%=path%>/style/base.css" type="text/css"  rel="stylesheet">
<script type="text/javascript" src="<%=path%>/js/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
	function load(obj,level){
		if(level==-1){
			obj=obj.substring(0,obj.lastIndexOf('/'));
		}
		$('#path').val(obj);
		sub.submit();
	}
	function common(){
		$('#path').val("<%=vv_path%>");
		$('#common').attr('checked','checked');
		sub.submit();
	}
	function filter(){
		var filter= $('#fileter').val();
		window.location.href="index.jsp?filter="+filter; 
	}
	function only(){
		var only= $('#fileter').val();
		window.location.href="index.jsp?only="+only; 
	}
	function reload(){
		window.location.href="index.jsp";
	}
	console.log('document.cookie:'+document.cookie);
	console.log('session.vv_path'+this.Session.vv_path);
</script>
</head>
<body>
	<form action="<%=path%>/MonitorServlet" name='sub'>
		路径：<input name="path" id="path" type="text"><input type="submit" value="切换路径">	
		<input type="checkbox" name='common' id='common'><label
			style="font-size: 12px">添加为常用路径</label><br> 
	<label style="font-size: 12px;">常用路径:</label>
		<%
			Iterator<String> it = Config.commonPath.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
		%>
		<span class="commonPath" onclick="load('<%=key%>','1')"><%=key%></span>
		<%
			}
		%>
		<br /> 
	</form>
	<button onclick="reload()">刷新</button><button onclick="load('<%=vv_path%>','-1')">上一级</button>
	<button onclick="common()">本级设为常用</button>
	【<input id='fileter'>】<button onclick="filter()">设置不显示</button>
	<button onclick="only()">设置只显示</button>
	<table cellpadding="0" cellspacing="0">
		<tr class='head'>
			<td>名称</td>
			<td>路径</td>
			<td>大小</td>
			<td>修改时间</td>
			<td>权限</td>
		</tr>
		<%
			if (v != null) {
				for (int i = 0; i < v.size(); i++) {
					if (".".equals(v.get(i).getFilename())
							|| "..".equals(v.get(i).getFilename())) {
						continue;
					}
					if (only == null || "".equals(only)|| "null".equals(only)) {//如果没有设置只显示
						if (filter != null && !"".equals(filter)) {
							String[] filters = filter.split(",");
							boolean flag = false;
							for (int j = 0; j < filters.length; j++) {
								if (v.get(i).getFilename().endsWith(filters[j])) {
									flag = true;
									break;
								}
							}
							if (flag) {
								continue;
							}
						}
					}else{
							String[] onlys = only.split(",");
							boolean flag = false;
							for (int j = 0; j < onlys.length; j++) {
								if (v.get(i).getFilename().endsWith(onlys[j])) {
									flag = true;
									break;
								}
							}
							if (!flag) {
								continue;
							}
					}
		%>
		<tr>
			<td
				<%if (v.get(i).getAttrs().getPermissionsString()
							.startsWith("d")) {%>
				class='pointer'
				onclick="load('<%="/".equals(vv_path) ? ("/" + v.get(i)
								.getFilename()) : (vv_path + "/" + v.get(i)
								.getFilename())%>','1')"
				<%}%>><%=v.get(i).getFilename()%></td>
			<td><%="/".equals(vv_path) ? ("/" + v.get(i)
							.getFilename()) : (vv_path + "/" + v.get(i)
							.getFilename())%></td>
			<td><%=StringUtils.long2Size(v.get(i).getAttrs()
							.getSize())%></td>
			<td><%=DateUtils.getDateStrFromDateStr(v.get(i)
							.getAttrs().getMtimeString(), DateUtils.format,
							DateUtils.format_yyyy_mm_dd_hh_mm_ss)%></td>
			<td><%=v.get(i).getAttrs().getPermissionsString()%></td>
		</tr>
		<%
			}
			}
		%>
	</table>
</body>
</html>